package com.fengniu.ejb.dao.tests;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import junit.framework.JUnit4TestAdapter;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.junit.Test;

public class SystemDataImporter extends EJBSuperTest {
	static Logger logger = Logger.getLogger(SystemDataImporter.class);

	private String fileHome = "D:/conanwork/fatcat-workspace/Com_NiuMoWang_EJB/data/";

	private String[] categoryFileNames = {// ---------------------------------------------------
			"category/area_category_all_china.xml"// ---------------------------------------------------
			,"category/area_category_anhui.xml"// ---------------------------------------------------
			,"category/area_category_beijing.xml"// ---------------------------------------------------
			,"category/area_category_beijing_1.xml"// ---------------------------------------------------
			,"category/area_category_chongqing.xml"// ---------------------------------------------------
			,"category/area_category_fujian.xml"// ---------------------------------------------------
			,"category/area_category_gansu.xml"// ---------------------------------------------------
			,"category/area_category_guangdong.xml"// ---------------------------------------------------
			,"category/area_category_guangdong_shenzhen.xml"// ---------------------------------------------------
			,"category/area_category_guangxi.xml"// ---------------------------------------------------
			,"category/area_category_guizhou.xml"// ---------------------------------------------------
			,"category/area_category_hainan.xml"// ---------------------------------------------------
			,"category/area_category_hangzhou.xml"// ---------------------------------------------------
			,"category/area_category_hebei.xml"// ---------------------------------------------------
			,"category/area_category_heilongjiang.xml"// ---------------------------------------------------
			,"category/area_category_henan.xml"// ---------------------------------------------------
			,"category/area_category_hubei.xml"// ---------------------------------------------------
			,"category/area_category_hunan.xml"// ---------------------------------------------------
			,"category/area_category_jiangsu.xml"// ---------------------------------------------------
			,"category/area_category_jiangxi.xml"// ---------------------------------------------------
			,"category/area_category_jiling.xml"// ---------------------------------------------------
			,"category/area_category_liaoning.xml"// ---------------------------------------------------
			,"category/area_category_nanjing.xml"// ---------------------------------------------------
			,"category/area_category_neimenggu.xml"// ---------------------------------------------------
			,"category/area_category_ningxia.xml"// ---------------------------------------------------
			,"category/area_category_qignhai.xml"// ---------------------------------------------------
			,"category/area_category_shandong.xml"// ---------------------------------------------------
			,"category/area_category_shanghai.xml"// ---------------------------------------------------
			,"category/area_category_shanghai_1.xml"// ---------------------------------------------------
			,"category/area_category_shanxi.xml"// ---------------------------------------------------
			,"category/area_category_shanxishanxi.xml"// ---------------------------------------------------
			,"category/area_category_sichuan.xml"// ---------------------------------------------------
			,"category/area_category_tianjing.xml"// ---------------------------------------------------
			,"category/area_category_wuhan.xml"// ---------------------------------------------------
			,"category/area_category_xinjiang.xml"// ---------------------------------------------------
			,"category/area_category_xizang.xml"// ---------------------------------------------------
			,"category/area_category_yunnan.xml"// ---------------------------------------------------
			,"category/area_category_zhejiang.xml"// ---------------------------------------------------
	// "category/product_book_category.xml",
	// //------------------------------------------
	// ----brand category should import first before other product category
	// "category/brand_cellphone_category.xml",
	// //------------------------------------------
//	 "category/product_cellphone_category.xml",
	// //------------------------------------------
	// "category/product_electronic_category.xml",
	// //------------------------------------------
	// "category/product_motherbaby_category.xml",//------------------------------------------
	// "category/product_musicvideo_category.xml",//------------------------------------------
	// "category/product_rawfood_category.xml",//------------------------------------------
//	"category/business_category.xml",// ------------------------------------------
//	 "category/website_category.xml"//------------------------------------------
	};

	private String[] siteFileNames = {// --------------------------------------------------- 
			"stocks/site_stocks.xml", "website/ws0101.xml"// ---------------------------------------------------
			,"website/ws0102.xml"// ---------------------------------------------------
			,"website/ws_bagua.xml"// ---------------------------------------------------
			,"website/ws_baozhi.xml"// ---------------------------------------------------
			,"website/ws_birdee.xml"// ---------------------------------------------------
			,"website/ws_boke.xml"// ---------------------------------------------------
			,"website/ws_bt.xml"// ---------------------------------------------------
			,"website/ws_caijing.xml"// ---------------------------------------------------
			,"website/ws_chengxu.xml"// ---------------------------------------------------
			,"website/ws_diannao.xml"// ---------------------------------------------------
			,"website/ws_dianshi.xml"// ---------------------------------------------------
			,"website/ws_dianzi.xml"// ---------------------------------------------------
			,"website/ws_EgnlisnMBA.xml"// ---------------------------------------------------
			,"website/ws_falv.xml"// ---------------------------------------------------
			,"website/ws_fangchan.xml"// ---------------------------------------------------
			,"website/ws_fangzhi.xml"// ---------------------------------------------------
			,"website/ws_football.xml"// ---------------------------------------------------
			,"website/ws_forum.xml"// ---------------------------------------------------
			,"website/ws_free.xml"// ---------------------------------------------------
			,"website/ws_fuzhuang.xml"// ---------------------------------------------------
			,"website/ws_game.xml"// ---------------------------------------------------
			,"website/ws_guanggao.xml"// ---------------------------------------------------
			,"website/ws_guofang.xml"// ---------------------------------------------------
			,"website/ws_guowai.xml"// ---------------------------------------------------
			,"website/ws_heike.xml"// ---------------------------------------------------
			,"website/ws_huagong.xml"// ---------------------------------------------------
			,"website/ws_huanbao.xml"// ---------------------------------------------------
			,"website/ws_huangye.xml"// ---------------------------------------------------
			,"website/ws_jiadian.xml"// ---------------------------------------------------
			,"website/ws_jianzhu.xml"// ---------------------------------------------------
			,"website/ws_jiaotong.xml"// ---------------------------------------------------
			,"website/ws_jiaoyu.xml"// ---------------------------------------------------
			,"website/ws_jixie.xml"// ---------------------------------------------------
			,"website/ws_kaoshi.xml"// ---------------------------------------------------
			,"website/ws_katong.xml"// ---------------------------------------------------
			,"website/ws_kexue.xml"// ---------------------------------------------------
			,"website/ws_liping.xml"// ---------------------------------------------------
			,"website/ws_liuxue.xml"// ---------------------------------------------------
			,"website/ws_liyou.xml"// ---------------------------------------------------
			,"website/ws_lottery.xml"// ---------------------------------------------------
			,"website/ws_love.xml"// ---------------------------------------------------
			,"website/ws_lunwen.xml"// ---------------------------------------------------
			,"website/ws_man.xml"// ---------------------------------------------------
			,"website/ws_nba.xml"// ---------------------------------------------------
			,"website/ws_nonglin.xml"// ---------------------------------------------------
			,"website/ws_picture.xml"// ---------------------------------------------------
			,"website/ws_qiche.xml"// ---------------------------------------------------
			,"website/ws_redian.xml"// ---------------------------------------------------
			,"website/ws_rencai.xml"// ---------------------------------------------------
			,"website/ws_ruanjianxiazai.xml"// ---------------------------------------------------
			,"website/ws_shangye.xml"// ---------------------------------------------------
			,"website/ws_shaonian.xml"// ---------------------------------------------------
			,"website/ws_shehui.xml"// ---------------------------------------------------
			,"website/ws_shenghuo.xml"// ---------------------------------------------------
			,"website/ws_sheshi.xml"// ---------------------------------------------------
			,"website/ws_shipin.xml"// ---------------------------------------------------
			,"website/ws_shishang.xml"// ---------------------------------------------------
			,"website/ws_shouji.xml"// ---------------------------------------------------
			,"website/ws_sousuo.xml"// ---------------------------------------------------
			,"website/ws_sport.xml"// ---------------------------------------------------
			,"website/ws_star.xml"// ---------------------------------------------------
			,"website/ws_suanming.xml"// ---------------------------------------------------
			,"website/ws_tuozi.xml"// ---------------------------------------------------
			,"website/ws_video.xml"// ---------------------------------------------------
			,"website/ws_wangshanggouwu.xml"// ---------------------------------------------------
			,"website/ws_wangzhan.xml"// ---------------------------------------------------
			,"website/ws_wap.xml"// ---------------------------------------------------
			,"website/ws_xenxue.xml"// ---------------------------------------------------
			,"website/ws_xiaofei.xml"// ---------------------------------------------------
			,"website/ws_xiaohua.xml"// ---------------------------------------------------
			,"website/ws_xiaoyuan.xml"// ---------------------------------------------------
			,"website/ws_xinwen.xml"// ---------------------------------------------------
			,"website/ws_yejing.xml"// ---------------------------------------------------
			,"website/ws_yiliao.xml"// ---------------------------------------------------
			,"website/ws_yingjian.xml"// ---------------------------------------------------
			,"website/ws_yingshi.xml"// ---------------------------------------------------
			,"website/ws_yinshi.xml"// ---------------------------------------------------
			,"website/ws_yishu.xml"// ---------------------------------------------------
			,"website/ws_yixue.xml"// ---------------------------------------------------
			,"website/ws_zhenfu.xml"// ---------------------------------------------------
			,"website/ws_zonghe.xml"// ---------------------------------------------------
 };//

	private String[] contentFileNames = { // ------------------------
	"products/cellphone/zol_cellphone.xml",// -----------------------------------------
			// "products/cellphone/cellphone_benmai.xml",//-----------------------------------------
			// "products/cellphone/cellphone_bodao.xml",//-----------------------------------------
			// "products/cellphone/cellphone_cect.xml",//-----------------------------------------
			// "products/cellphone/cellphone_daxian.xml",//-----------------------------------------
			// "products/cellphone/cellphone_dongzhi.xml",//-----------------------------------------
			// "products/cellphone/cellphone_dopodo.xml",//-----------------------------------------
			// "products/cellphone/cellphone_erison.xml",//-----------------------------------------
			// "products/cellphone/cellphone_fantai.xml",//-----------------------------------------
			// "products/cellphone/cellphone_heimei.xml",//-----------------------------------------
			// "products/cellphone/cellphone_hengjiweiye.xml",//-----------------------------------------
			// "products/cellphone/cellphone_huipu.xml",//-----------------------------------------
			// "products/cellphone/cellphone_jijia.xml",//-----------------------------------------
			// "products/cellphone/cellphone_jingci.xml",//-----------------------------------------
			// "products/cellphone/cellphone_konka.xml",//-----------------------------------------
			// "products/cellphone/cellphone_langxun.xml",//-----------------------------------------
			// "products/cellphone/cellphone_lenovo.xml",//-----------------------------------------
			// "products/cellphone/cellphone_lg.xml",//-----------------------------------------
			// "products/cellphone/cellphone_mingji.xml",//-----------------------------------------
			// "products/cellphone/cellphone_motorora.xml",//-----------------------------------------
			// "products/cellphone/cellphone_nec.xml",//-----------------------------------------
			// "products/cellphone/cellphone_nokia.xml",//-----------------------------------------
			// "products/cellphone/cellphone_samsum.xml",//-----------------------------------------
			// "products/cellphone/cellphone_sanling.xml",//-----------------------------------------
			// "products/cellphone/cellphone_sanyang.xml",//-----------------------------------------
			// "products/cellphone/cellphone_sk.xml",//-----------------------------------------
			// "products/cellphone/cellphone_songxia.xml",//-----------------------------------------
			// "products/cellphone/cellphone_sony.xml",//-----------------------------------------
			// "products/cellphone/cellphone_sonyerison.xml",//-----------------------------------------
			// "products/cellphone/cellphone_tcl.xml",//-----------------------------------------
			// "products/cellphone/cellphone_tianshida.xml",//-----------------------------------------
			// "products/cellphone/cellphone_utstarcom.xml",//-----------------------------------------
			// "products/cellphone/cellphone_weikai.xml",//-----------------------------------------
			// "products/cellphone/cellphone_xiapu.xml",//-----------------------------------------
			// "products/cellphone/cellphone_xiaxin.xml",//-----------------------------------------
			// "products/cellphone/cellphone_ximenzi.xml",//-----------------------------------------
			// "products/cellphone/cellphone_yinghuaok.xml",//-----------------------------------------
			// "products/cellphone/cellphone_zte.xml",//-----------------------------------------

			// ------------------------------------------------------------------------------
			// "products/rawfood/rawfood_chaye.xml",
			// "products/rawfood/rawfood_chuqindanpin.xml",
			// "products/rawfood/rawfood_liangyousiliao.xml",
			// "products/rawfood/rawfood_shipin.xml",
			// "products/rawfood/rawfood_shucai.xml",
			// "products/rawfood/rawfood_shuichanpin.xml",
			// "products/rawfood/rawfood_shuiguo.xml",
			// ------------------------------------------------------------------------------
			"products/coupon/store_coupon.xml",

	};

	private String[] content2CategoryFileNames = { // ---------------------------------
	"products/cellphone/zol_cellphone_2_category_1.xml", "products/cellphone/zol_cellphone_2_category_2.xml" };

	private String[] site2CategoryFileNames = {// --------------------------------------
	"stocks/site_stock_2_category.xml" // -------------------------------------------
	};

	private String[] site2ContentFileNames = {// -------------------------------------------
	"stocks/site_stock_2_cellphone.xml" // ------------------------------------------
	};

	private String[] userFileNames = {// ------------------------------------------------------
	"user/user_sample.xml" };

	private String[] user2ContentFileNames = {
	// "user/user_2_word.xml", "user/user_2_article.xml"
	};

	private String[] user2SiteFileNames = { "user/user_2_store.xml" };

	private String[] sampleDataFileNames = {//---------------------------------------------------- 
			"sample/horde.xml", // ------------------------------------------------------
			"sample/product.xml", // ------------------------------------------------------
			"sample/store.xml",// ------------------------------------------------------
			"sample/website.xml",// ------------------------------------------------------
			"sample/product2category.xml",// ------------------------------------------------------
			"sample/store2areacategory.xml",// ------------------------------------------------------
			"sample/store2bizcategory.xml",// ------------------------------------------------------
			"sample/horde2category.xml",// ------------------------------------------------------
			"sample/store2product.xml",// ------------------------------------------------------
			"sample/website2category.xml"// ------------------------------------------------------
	};

	public SystemDataImporter() {
		super();
	}

	public static void main(String... args) {
		junit.textui.TestRunner.run(suite());
	}

	public static junit.framework.Test suite() {
		return new JUnit4TestAdapter(SystemDataImporter.class);
	}

	@Test
	public void testImport() {
		importCategories();
		// importContents();
		// importContent2Categorys();
//		 importSites();
		// importSite2Categorys();
		// importSite2Contents();
		// importUsers();
		// importUser2Sites();
		// importUser2Contents();
//		importSampleDatas();
	}

	private void importSampleDatas() {
		for (int i = 0; i < sampleDataFileNames.length; i++) {
			importDataFromFile(fileHome + sampleDataFileNames[i]);
		}
	}

	private void importCategories() {
		for (int i = 0; i < categoryFileNames.length; i++) {
			importDataFromFile(fileHome + categoryFileNames[i]);
		}
	}

	public void importContents() {
		for (int i = 0; i < contentFileNames.length; i++) {
			importDataFromFile(fileHome + contentFileNames[i]);
		}
	}

	public void importContent2Categorys() {
		for (int i = 0; i < content2CategoryFileNames.length; i++) {
			importDataFromFile(fileHome + content2CategoryFileNames[i]);
		}
	}

	public void importSites() {
		for (int i = 0; i < siteFileNames.length; i++) {
			importDataFromFile(fileHome + siteFileNames[i]);
		}
	}

	public void importSite2Categorys() {
		for (int i = 0; i < site2CategoryFileNames.length; i++) {
			importDataFromFile(fileHome + site2CategoryFileNames[i]);
		}
	}

	public void importSite2Contents() {
		for (int i = 0; i < site2ContentFileNames.length; i++) {
			importDataFromFile(fileHome + site2ContentFileNames[i]);
		}
	}

	public void importUsers() {
		for (int i = 0; i < userFileNames.length; i++) {
			importDataFromFile(fileHome + userFileNames[i]);
		}
	}

	public void importUser2Sites() {
		for (int i = 0; i < user2SiteFileNames.length; i++) {
			importDataFromFile(fileHome + user2SiteFileNames[i]);
		}
	}

	public void importUser2Contents() {
		for (int i = 0; i < user2ContentFileNames.length; i++) {
			importDataFromFile(fileHome + user2ContentFileNames[i]);
		}
	}

	private void importDataFromFile(String fileName) {
		if (logger.isInfoEnabled()) {
			logger.info("Loading properties file from " + fileName);
		}
		logger.info("Loading properties file from " + fileName);

		// Read from an input stream
		try {
			InputStream is = new BufferedInputStream(new FileInputStream(fileName));
			logger.info("The sequence of records is very important,that means the previous one will be saved first "
					+ "and the later ones maybe depends on the previous one. "
					+ "Entity name first character must be capitalized and the fields must be lowcase");

			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(is);
			
			dataImportBusinessRemote.importData(document.asXML());
//			dataImportBusinessRemote.importData(document);

			logger.info("sleep 3 seconds........");
			Thread.sleep(3000);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}

	/**
	 * Import data from text
	 * 
	 * @param text
	 * @throws IOException
	 * @throws DocumentException
	 * @throws InterruptedException
	 */
	public void importDataFromText(String text) throws IOException, DocumentException, InterruptedException {
		// If you have some XML as a String you can parse it back into a
		// Document again using the helper method DocumentHelper.parseText()

		// Document document = DocumentHelper.parseText(text);
		// treeWalk(document);
	}

}